#include "FileWriter.h"


extern int SIZE_OF_SYMBOL;


FileWriter::FileWriter(char* szFileName,WT_TYPE eWtType)
{
	m_pFile=fopen(szFileName,"wb");

	switch (eWtType)
	{
	case WT_ZIP:
		InitZipFile();
		break;
	case WT_DST:
		InitDstFile();
		break;
	}

	m_iBitSeq=0;
	m_iBitSeqLen=0;
}


FileWriter::~FileWriter()
{
	while (m_iBitSeqLen>0)
		AddBit(0);
	fclose(m_pFile);
}


void FileWriter::AddBit(int iBit)
{
	m_iBitSeq|=iBit<<8;
	m_iBitSeq>>=1;
	m_iBitSeqLen++;
	if (m_iBitSeqLen==8)
	{
		fwrite(&m_iBitSeq,1,1,m_pFile);
		m_iBitSeqLen=0;
	}
}


void FileWriter::AddSymbol(int iSymbol)
{
	fwrite(&iSymbol,SIZE_OF_SYMBOL,1,m_pFile);
}


void FileWriter::InitZipFile()
{
	fwrite(&m_iSymbolsRange,sizeof(int),1,m_pFile);
	fwrite(&m_iNumOfSymbols,sizeof(int),1,m_pFile);

	for (int i=0; i<m_iNumOfSymbols; i++)
	{
		AddSymbol(m_aSymbolVector[i]);
		fwrite(m_aSymCntVector+i,sizeof(int),1,m_pFile);
	}
}


void FileWriter::InitDstFile()
{
}
